package com.tanhua.app.controller;

import com.tanhua.app.service.QuanziService;
import com.tanhua.domain.mongo.Publish;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
public class QuanziController {

    @Autowired
    private QuanziService quanziService;

    @PostMapping(value = "/movements", name = "发布动态")
    public ResponseEntity publishMovements(Publish publish, MultipartFile[] imageContent) throws IOException {

        //  给大数据做铺垫，指定动态id
        ObjectId publishId = ObjectId.get();
        publish.setId(publishId);

        // 调用service保存
        quanziService.publishMovements(publish, imageContent);

        return ResponseEntity.ok(null);
    }

    @GetMapping(value = "/movements/all", name = "查询用户动态")
    public ResponseEntity findAlbumByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
            Long userId) {
        return quanziService.findAlbumByPage(pageNum, pageSize, userId);
    }

    @GetMapping(value = "/movements", name = "查询好友动态")
    public ResponseEntity findTimeLineByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return quanziService.findTimeLineByPage(pageNum, pageSize);
    }

    @GetMapping(value = "/movements/recommend", name = "查询推荐动态")
    public ResponseEntity findRecommendByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return quanziService.findRecommendByPage(pageNum, pageSize);
    }


    @GetMapping(value = "/movements/{publishId}/like",name = "动态点赞")
    public ResponseEntity likePublish(@PathVariable String publishId){

        return quanziService.likePublish(publishId);
    }

    @GetMapping(value = "/movements/{publishId}/dislike",name = "动态取消点赞")
    public ResponseEntity dislikePublish(@PathVariable String publishId){

        return quanziService.dislikePublish(publishId);
    }


    @GetMapping(value = "/movements/{publishId}/love",name = "动态喜欢")
    public ResponseEntity lovePublish(@PathVariable String publishId){

        return quanziService.lovePublish(publishId);
    }



    @GetMapping(value = "/movements/{publishId}/unlove",name = "动态取消喜欢")
    public ResponseEntity unlovePublish(@PathVariable String publishId){

        return quanziService.unlovePublish(publishId);
    }


    @GetMapping(value = "/movements/{publishId}",name = "单条动态")
    public ResponseEntity findPublishVoById(@PathVariable String publishId){
        return quanziService.findPublishVoById(publishId);
    }
}
